<?php
	function clicklien($url)
	{ 
		$in = array( '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`((?<!//)(www\.\S+[[:alnum:]]/?))`si' ); 
		$out = array( '<a href="$1">$1</a>', '<a href="http://$1">$1</a>' ); 
		return preg_replace($in,$out,$url); 
	}
	
	// r = resultats SQL, c = commentaires, f = formulaire
	function display ($r, $c, $f, $admin=false)
	{
		global $HTTP_COOKIE_VARS;
		$dir = "http://hurmpf.free.fr/img/";
		$dirR = "http://hurmpf.free.fr/img/resized/";
		if(mysql_num_rows($r)>0)
		{
			while($row = mysql_fetch_array($r))
			{
				$id = $row['id'];
				$dateString = date('d/m/Y, H:i',strtotime($row['date']));
				$titre = $row['titre'];
				$texte = $row['texte'];
				
				$texte = preg_replace('/\[imglink\=([^\]]+)\]/','<a href="'.$dir.'\\1"><img src="'.$dirR.'\\1"></a>',$texte);
				$texte = preg_replace('/\[img\=([^\]]+)\]/','<img src="'.$dir.'\\1">',$texte);
							
				echo '<div class="post">'."\n";
				echo '<h1 class="postTitre"><a href="?p='.$id.'">'.$titre.'</a></h1>'."\n";
				echo '<div class="postTexte">'.$texte.'</div>'."\n";
				echo '<div class="postDate">'.$dateString;
				if($admin) echo ' (<a href="admin/?id='.$id.'">editer</a>)'."\n";
				echo '</div>'."\n";
				
				// commentaires
				if($c)
				{
					$sql2 = 'SELECT * FROM comments WHERE ref='.$id.' ORDER BY date';
					$req2 = mysql_query($sql2) or die('Erreur SQL !<br>'.$sql2.'<br>'.mysql_error());
					echo "<a name=\"comments\"><hr></a>\n";
					if(mysql_num_rows($req2)>0) echo "<h3>Commentaires</h3>\n";
					else echo "<h3>Aucun commentaire</h3>\n";
					while($row2 = mysql_fetch_array($req2))
					{
						$id2 = $row2['id'];
						$dateString2 = date('d/m/Y, H:i',strtotime($row2['date']));
						$nom2 = $row2['nom'];
						$web = $row2['web'];
						$texte2 = nl2br(clicklien($row2['texte']));
						$defaultavatar = urlencode('http://hurmpf.free.fr/pix.gif');
						echo '<a name="'.$id2.'"></a>';
						echo '<div class="comment">';
						if($row2['email']!="")
						{
							$email = md5($row2['email']);
							$avatar = 'http://www.gravatar.com/avatar.php?gravatar_id='.$email.'&amp;size=60&amp;default='.$defaultavatar;
							echo '<img src="'. $avatar .'" class="commentAvatar"/>';
						}
						if(!empty($web) && substr($web,0,7)!="http://" && substr($web,0,8)!="https://")
							$web = "http://".$web;
						if(!empty($web)) $nom2 = '<a href="'.$web.'">'.$nom2.'</a>';
						echo '<div class="commentNom">'.$nom2.'</div>'."\n";
						echo '<div class="commentDate">('.$dateString2.')</div>'."\n";
						if($admin) echo '(<a href="admin/commentaire.php?id='.$id2.'">editer</a>)'."\n";
						echo '<div class="commentTexte">'.$texte2.'</div>'."\n";
						echo '</div>'."\n";
					}
				}
				else
				{
					$sql2 = 'SELECT id FROM comments WHERE ref='.$id;
					$req2 = mysql_query($sql2) or die('Erreur SQL !<br>'.$sql2.'<br>'.mysql_error());
					$nc = mysql_num_rows($req2);
					if($nc == 0)
						echo '<div class="addcommentlink"><a href="?p='.$id.'#form" rel="nofollow">Ajouter un commentaire</a></div>'."\n";
					else
						echo '<div class="addcommentlink"><a href="?p='.$id.'#comments" rel="nofollow">'.$nc.' commentaire'.($nc>1?'s':'')."</a></div>\n";
				}
				// formulaire commentaires
				if($f)
				{
					$nom = $HTTP_COOKIE_VARS["nom"];
					$email = $HTTP_COOKIE_VARS["email"];
					$web = $HTTP_COOKIE_VARS["web"];
					if(!empty($nom) || !empty($email) || !empty($web)) $nospam = true;
					?>
					<a name="form"><hr></a>
					<div id="myComment">
					<h3>Laisser un commentaire</h3>
					<form id="commentform">
					Nom : <input type="textfield" id="commentName" value="<?php echo $nom; ?>"> <span class="precision">(obligatoire)</span><br />
					Email : <input type="textfield" id="commentEmail" value="<?php echo $email; ?>"> <span class="precision">(facultatif, sert à afficher un <a href="http://www.gravatar.com">Gravatar</a>)</span><br />
					Site web : <input type="textfield" id="commentWeb" value="<?php echo $web; ?>"> <span class="precision">(facultatif)</span><br />
					<?php
						if($nospam)
							echo '<input type="hidden" id="commentSpam" value="plop">'."\n";
						else
							echo 'Antispam (écrire plop) : <input type="textfield" id="commentSpam"> <span class="precision">(obligatoire)</span><br />'."\n";
					?>
					<textarea id="commentText" name="message" rows="15" cols="70"></textarea><br />
					<div class="precision" style="float: right;"><input type="checkbox" id="save"> Enregistrer les informations dans un cookie</div>
					<input type="button" value="Envoyer" id="sendCommentButton" onClick="sendComment(<?php echo $id; ?>)">
					</form>
					</div>
					<?php
				}
				//else echo '<div class="addcommentlink"><a href="?p='.$id.'#form" rel="nofollow">Ajouter un commentaire</a></div>'."\n";
					
				echo '</div>'."\n";
			}
		}
		else
		{
			include("notfound.php");
		}
	}
?>